import request from '@/utils/request'
import axios from 'axios'


export const courseApi = {
  getTeacherCourseList(teacherId) {
    // 添加/spoc前缀，修正course拼写
    return request.get(`/spoc/vod/course/teacherCourseList/${teacherId}`)
  },
  saveCourse(data) {
    return request.post('/vod/course/save', data)
  },
  updateCourse(data) {
    return request.post('/vod/course/update', data)
  },
  deleteCourse(id) {
    return request.delete(`/vod/course/remove/${id}`)
  },
  getCourseInfo(teacherId) {
    return request.get(`/vod/course/getInfoByTeacher/${teacherId}`)
  },
  publishCourse(id) {
    return request.post(`/vod/course/publishCourse/${id}`)
  },
   // 新增获取课程详情方法
  // 增强课程详情获取方法
getCourseDetail(courseId) {
  console.log('[CourseAPI] 获取课程详情', { courseId })
  return request.get(`/api/course-detail/${courseId}`).then(response => {
    console.log('[CourseAPI] 课程详情原始响应:', response)
    const detail = response?.data || response
    if (!detail) {
      throw new Error('课程详情为空')
    }
    return { 
      data: {
        ...detail,
        chapters: Array.isArray(detail.chapters) ? detail.chapters : [],
        comments: Array.isArray(detail.comments) ? detail.comments : []
      }
    }
  }).catch(error => {
    console.error('[CourseAPI] 获取课程详情错误:', error)
    return { data: null }
  })
},

// 获取课程章节树
  getChapterTree(courseId) {
    return request.get(`/spoc/vod/chapter/getNestedTreeListByTeacher/${courseId}`)
  },
  
  // 创建章节
  createChapter(data) {
    return request.post('/spoc/vod/chapter', data)
  },
  
  // 更新章节
  updateChapter(chapterId, data) {
    return request.put(`/spoc/vod/chapter/${chapterId}`, data)
  },
  
  // 删除章节
  deleteChapter(chapterId) {
    return request.delete(`/spoc/vod/chapter/${chapterId}`)
  },
  
  // 更新章节顺序
  updateChapterOrder(data) {
    return request.put('/spoc/vod/chapter/order', data)
  },
  
  // 获取章节详情
  getChapterDetail(chapterId) {
    return request.get(`/spoc/vod/chapter/get/${chapterId}`)
  },
  
  // 更新章节索引
  updateChapterIndex(courseId, oldIndex, newIndex) {
    return request.post('/spoc/vod/chapter/updateIndex', null, {
      params: { courseId, oldIndex, newIndex }
    })
  },
  // 获取视频信息
  getVideoInfo(courseId, chapterId) {
    return request.get(`/spoc/vod/chapter/video-info/${courseId}/${chapterId}`)
  },

  // 更新课程信息（仅名称和简介）
// 更新课程信息（仅名称和简介）
updateCourseInfo(courseId, data) {
  console.log('[DEBUG] 请求数据:', {
    url: `/spoc/vod/course/${courseId}`,
    method: 'patch',
    data: data,
    headers: request.defaults.headers // 查看默认请求头
  });
  
  return request.patch(`/spoc/vod/course/${courseId}`, data)
    .then(response => {
      console.log('[DEBUG] 响应数据:', response);
      return response;
    })
    .catch(error => {
      console.error('[DEBUG] 请求错误:', error);
      throw error;
    });
},
  
  // 获取章节学习进度
  getChapterProgress(studentId, courseId, chapterId) {
    return axios.get('/api/learning-progress/chapter', {
      params: { studentId, courseId, chapterId }
    })
  },
  
  // 更新学习进度
  updateLearningProgress(dto) {
    return axios.post('/api/learning-progress/update', dto)
  },
  
  // 标记章节为已完成
  markChapterAsCompleted(studentId, courseId, chapterId) {
    return axios.post('/api/learning-progress/complete', null, {
      params: { studentId, courseId, chapterId }
    })
  },
}